﻿//
// Thanks System Powered By AvacWeb.
// Copyright آ© 2012, All Rights reserved. No Distribution Allowed.
// Domain Access: http://www.4egy.net/
// User number 110
// Account Type: Free
// Authorization Serial: ef34ba6ac48934af27ccd6e7274f274a
//
(function() {
	//some extra cheap protection :P 
	if(window.location.hostname != 'www.4egy.net') return;

	window.$avacweb_thanks = {
		thanks_ready : false,
		posts_ready : false,
		url : 'http://thanks.avacweb.net/t110?auth=ef34ba6ac48934af27ccd6e7274f274a',
		domain : 'http://www.4egy.net/',
		
		regex : {
			'home' : /^(forum)?$/,
			'topic' : /^t(\d+)(p\d+)?-.*/,
			'profile' : /^\/?u(\d+)(wall|stats|friends|contact)?$/,
			'post' : /^p?(\d+)/
		},
		thanks : {},
		post_details : {}
	};
	
	$avacweb_thanks.request = function(data) {
		var url = $avacweb_thanks.url + '&' + data;
		var xhr = new XMLHttpRequest;
		if ("withCredentials" in xhr) {
			xhr.open('GET', url, true);
		}
		else if (typeof XDomainRequest != "undefined") {
			xhr = new XDomainRequest;
			xhr.open('GET', url);
		} 
		else { 
			xhr = null;
		}
		if (xhr) { 
			xhr.onload = function() { eval(xhr.responseText); }; 
			xhr.send(); 
		} 
		else {
			$avacweb_thanks.backup_request(url);
		}
	};
	
	$avacweb_thanks.backup_request = function(url) {
		var s = document.createElement('script');
		s.src = url + '&backup=1';
		s.type = 'text/javascript';
		s.async = 'async';
		document.getElementsByTagName('head')[0].appendChild(s);
	}
	
	$avacweb_thanks.stripTags = function(str) {
		return str.replace(/\<\/?[^\>]+\>/g, '').replace(/(^\s+|\s+$)/g, '');
	};
	
	$avacweb_thanks.thank = function(pid) {
		var info = $avacweb_thanks.post_details['p'+pid];
		if(!info) return;
		$avacweb_thanks.request("mode=add&p="+pid+"&t="+window.location.pathname.substr(1)+"&u="+$avacweb_thanks.USERNAME+"&ui="+$avacweb_thanks.USERID+"&a="+info.name+"&ai="+info.id);
	};
	
	$avacweb_thanks.show_thanks = function() {
		if(!$avacweb_thanks.thanks_ready || !$avacweb_thanks.posts_ready) return setTimeout($avacweb_thanks.show_thanks, 0300);
		var t = jQuery('.avacweb_thanks_box');
		for(var i = 0, l = t.length; i<l; i++) {
			var p = t[i].id.replace('avacweb_thanks_box_', 'p'), names = 'لا يوجد إعجابات بعد.';
			var box = document.getElementById('avacweb_thanks_names_'+p.substr(1));
			if(p in $avacweb_thanks.thanks) {
				names = $avacweb_thanks.thanks[p].join(', ');
				if(names.indexOf('>' + $avacweb_thanks.USERNAME + '<') !== -1) document.getElementById('avacweb_thanks_btn_'+p.substr(1)).className += ' alreadyThanked';
				box.className = box.className.replace(' nothanks', '');
			}
			else {
				box.className += ' nothanks';
			}
			box.innerHTML = names;
		}
	};
	
	$avacweb_thanks.can_thank = function(aid) {
		if($avacweb_thanks.USERID < 0 || $avacweb_thanks.USERNAME == 'Guest') return false;
		if(aid) {

			if( $avacweb_thanks.USERID === aid ) return false;
		}
		return true;
	};
	
	$avacweb_thanks.init = function() {
		var info = document.getElementById('avacweb_thanks_user_110');
		if(!info) return false;
		info.style.display = 'none';
		info = info.firstChild;
		if(info && info.href) {
			var href = info.href.substr( info.href.lastIndexOf('/') );
			$avacweb_thanks.USERID = href.replace( /[^&]+&u=(-?\d+)$/, '$1');
			if((/^-?\d+$/).test($avacweb_thanks.USERID)) {
				$avacweb_thanks.USERID = parseInt( $avacweb_thanks.USERID );
			}
			else {
				return false;
			}
			$avacweb_thanks.USERNAME = $avacweb_thanks.stripTags( info.innerHTML );
			$avacweb_thanks.addons(); //init any addons
			return true;
		}
		return false;
	};
    
	$avacweb_thanks.addons = function() {
		
	}
	
	
	jQuery(function() {
		if(!$avacweb_thanks.init()) return;
	
		var post = jQuery('.avacweb_thanks_box');
		for(var i = 0, l = post.length; i<l; i++) {
			var pid = post[i].id.replace('avacweb_thanks_box_', '');
			var author = post[i].getElementsByTagName('a')[0];
			if(!author) continue;
			var author_id = author.href.substr( author.href.lastIndexOf('/') ).replace($avacweb_thanks.regex.profile, '$1');
			author = $avacweb_thanks.stripTags(author.innerHTML);
			$avacweb_thanks.post_details['p'+pid] = {id: parseInt(author_id), name: author};
			var thankbtn = document.getElementById('avacweb_thanks_btn_'+pid);
			var thankbox = document.getElementById('avacweb_thanks_names_'+pid);
			if($avacweb_thanks.can_thank(parseInt(author_id))) {
				thankbtn.innerHTML = 'أعجبنى';
                thankbtn.style.display = '';
				jQuery(thankbtn).click(function() {
					$avacweb_thanks.thank( this.id.replace('avacweb_thanks_btn_', '') );
				});
			}
		}
		$avacweb_thanks.posts_ready = true;
	});
	
	
	if($avacweb_thanks.regex.topic.test(window.location.pathname.substr(1))) {
		$avacweb_thanks.request('mode=data&t='+window.location.pathname);
	};
    
	
    
	if(!Array.prototype.indexOf) {
		Array.prototype.indexOf = function(item) {
			for(var i = this.length; i; i--) {
				if(this[i-1] === item) return i - 1;
			}
			return -1;
		}
	}
})();